/*
	Copyright (C) 2010 2011 Rémi Thebault

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef TASKTREEITEM_H_
#define TASKTREEITEM_H_


#include <QList>
#include <QtAlgorithms>
#include <QVariant>

#include <procycle/cycle.h>
#include <procycle/task.h>


class TaskTreeItem {
public:
	TaskTreeItem(PC::Task* task, TaskTreeItem* parent=0);


	virtual ~TaskTreeItem() {
		qDeleteAll(m_children);
	}

	void appendChild (TaskTreeItem* child) {
		child->setParent(this);
		m_children.push_back(child);
	}


	void insertChild(TaskTreeItem* child, int pos) {
		child->setParent(this);
		m_children.insert(pos, child);
	}

	TaskTreeItem* removeChild(int pos) {
		TaskTreeItem* item = m_children.at(pos);
		m_children.removeAt(pos);
		return item;
	}

    virtual TaskTreeItem* child(int row) {
        return m_children.value(row, 0);
	}

    virtual int childCount() const {
		return m_children.count();
	}

    virtual QVariant data(PC::Task::Data td) const;
    virtual bool setData(PC::Task::Data td, QVariant value);


    virtual int row() const  {
		if (m_parent) {
			return m_parent->m_children.indexOf(const_cast<TaskTreeItem*>(this));
		}
		return 0;
	}

    virtual Qt::ItemFlags flags(PC::Task::Data td) const;

	TaskTreeItem* resolvItem(QString r_tbsId);


	PC::Task* task() {
		return m_task;
	}
	void setTask(PC::Task* task) {
		m_task = task;
	}


	TaskTreeItem* parent() {
		return m_parent;
	}
	void setParent(TaskTreeItem* parent) {
		m_parent = parent;
	}



protected:
	PC::Task*				m_task;
	TaskTreeItem*			m_parent;

	QList<TaskTreeItem*>	m_children;
};




class AppendTaskTreeItem : public TaskTreeItem {
public:
    AppendTaskTreeItem(PC::Cycle *cycle, TaskTreeItem* parent) :
        TaskTreeItem(0, parent),
        m_cycle(cycle)
    {}

    int childCount() const { return 0; }
    TaskTreeItem *child (int row) { return 0; }

    QVariant data(PC::Task::Data td) const {
        if (td == PC::Task::DATA_NUM) {
            return QVariant(-1);
        }
        return TaskTreeItem::data(td);
    }
    bool setData(PC::Task::Data td, QVariant value);

    int row() const {
        return m_cycle->taskCount();
    }

    Qt::ItemFlags flags(PC::Task::Data td) const;

private:
    PC::Cycle *m_cycle;
};




class RootTaskTreeItem : public TaskTreeItem {
public:
    RootTaskTreeItem(PC::Cycle *cycle) :
        TaskTreeItem(cycle, 0),
        m_appendItem(cycle, this)
    {
    }

    int childCount() const {
        return m_children.count() + 1;
    }

    TaskTreeItem *child (int row) {
        if (row >= m_children.count()) {
            return &m_appendItem;
        }
        return m_children.value(row);
    }

private:
   AppendTaskTreeItem m_appendItem;
};



#endif /* TASKTREEITEM_H_ */
